package com.itheima.controller;

import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckItemService;
import com.itheima.health.utils.resources.MessageConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController   //  @Controller +@ResponseBody
@Api(tags = "检查项管理")
public class CheckItemController {

    @Reference
    private CheckItemService checkItemService;

    @Autowired
    private RedisTemplate redisTemplate;

    //   查询所有检查项 。。。
    @GetMapping("checkitem/findAll")
    @ApiOperation(value = "查询检查项",notes = "查询所有检查项信息")
    @ApiResponses({
            @ApiResponse(code=400,message="请求参数没填好"),
            @ApiResponse(code=404,message="请求路径没有或页面跳转路径不对"),
            @ApiResponse(code=500,message="服务端代码异常")
    })
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
       public Result findAll(){
        try {
            List<CheckItem> list = checkItemService.list();
            return  new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

     @PostMapping("checkitem/findPage")
     @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try {
           PageResult pageResult =  checkItemService.findPage(queryPageBean);
            return  new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }


    @PostMapping("checkitem/add")
    public Result add(@RequestBody CheckItem checkItem){
        try {
            //   saveOrUpdate()    完成新增和修改 两个实现
            //    checkitem  没有id    =   insert  操作
            //  checkitem   有id     =  数据库查询该id对应得记录   update 操作
              checkItemService.saveOrUpdate(checkItem);
              redisTemplate.delete("setmealInfo");
              if(StringUtils.isEmpty(checkItem.getId())){
                  //  insert
                  return  new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
              }else{
                  // update
                  return  new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
              }

        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,"更新失败！");
        }
    }

    @GetMapping("checkitem/findCheckItemById/{id}")
    public Result findCheckItemById(@PathVariable("id") int id){
        try {
            CheckItem checkItem = checkItemService.getById(id);
            return  new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS,checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
    }

    @DeleteMapping("checkitem/deleteCheckItemById/{id}")
    public Result  deleteCheckItemById(@PathVariable("id") int id){
        try {
             CheckItem checkItem = new CheckItem();
             checkItem.setIs_delete(1);  //  逻辑删除
             checkItem.setId(id);
             checkItemService.updateById(checkItem);
            return  new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
    }

}
